#### https://r2clr.codeplex.com/
rclrRoot = 'f:/codeplex/r2clr' # set this to your own location

library(rClr)
clrInit()

# Use the couple of following lines if you want to run basic unit tests prior to undertaking the tutorial.
# library(testthat)
# test_package('rClr')

# clrLoadAssembly(dllFilename=file.path( rclrRoot, 'doc/DataConversionSample/bin/Release/DataConversionSample.dll'))
# or
clrLoadAssembly(dllFilename=file.path( rclrRoot, 'doc/DataConversionSample/bin/Debug/DataConversionSample.dll'))

clrGetLoadedAssemblies()
# [1] "mscorlib"              "ClrFacade"             "System.Core"           "System"               
# [5] "RDotNet"               "RDotNet.NativeLibrary" "DataConversionSample" 

typename = clrGetTypesInAssembly('DataConversionSample')
typename = "rClr.Samples.DataConversionSample,DataConversionSample"
obj = clrNew(typename)
obj
clrReflect(obj)
# $Methods
# [1] "DateTimeToString"     "Equals"               "GetDate"              "GetHashCode"          "GetOneDimArray"      
# [6] "GetOneDimStringArray" "GetRectArray"         "GetType"              "ToString"            
# $Fields
# character(0)
# $Properties
# character(0)
clrGetMemberSignature(obj, "GetDate")
# [1] "Static, Method: DateTime GetDate, String"

str(clrCallStatic(typename, "GetDate", '2001-02-03'))
# Date[1:1], format: "2001-02-03"

str(clrCallStatic(typename, "DateTimeToString", as.Date('2001-02-03')))
#  chr "3/02/2001 12:00:00 AM"

str(clrCallStatic(typename, "GetOneDimStringArray", as.integer(5)))
#  chr [1:5] "0" "1" "2" "3" "4"

str(clrCallStatic(typename, "GetOneDimArray", as.integer(5)))
#  num [1:5] 0 1.1 2.2 3.3 4.4

str(clrCallStatic(typename, "GetRectArray", as.integer(3), as.integer(5)))
# num [1:3, 1:5] 0 1.1 2.2 0.22 1.32 2.42 0.44 1.54 2.64 0.66 ...

# What a .NET object looks like
clrCallStatic('System.Environment', 'get_Version')
# Dictionaries are not yet converted to R lists, by default.
clrCallStatic('System.Environment', 'GetEnvironmentVariables')
